package cn.yh.register.hosp.controller;

import cn.yh.register.common.result.Result;
import cn.yh.register.common.util.MD5;
import cn.yh.register.hosp.service.HospitalSetService;
import cn.yh.register.model.hosp.HospitalSet;
import cn.yh.register.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
// @CrossOrigin    //允许跨域访问
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //1 查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result<?> findAllHospitalSet() {
        //int i = 1 / 0;
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    //2.逻辑删除医院设置
    @ApiOperation(value = "逻辑删除医院设置")
    @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", paramType = "path")
    @DeleteMapping("/{id}")
    public Result<HospitalSet> removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();

    }

    //3.条件查询带分页
    @ApiOperation(value = "条件查询带分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "hospitalSetQueryVo", value = "查询条件", dataType = "",paramType = "body")
    })
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result<?> findPageHospSet(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);
        LambdaQueryWrapper<HospitalSet> wrapper = null;
        //构建条件
        if (hospitalSetQueryVo != null) {
            wrapper = new LambdaQueryWrapper<>();

            wrapper.like(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname()), HospitalSet::getHosname, hospitalSetQueryVo.getHosname());

            wrapper.like(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()), HospitalSet::getHoscode, hospitalSetQueryVo.getHoscode());
        }


        //调用方法实现分页查询
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);

        //返回结果
        return Result.ok(pageHospitalSet);
    }

    //4 添加医院设置
    @ApiOperation(value = "添加医院设置")
    @ApiImplicitParam(name = "hospitalSet", value = "添加的医院配置", dataType = "JSON", paramType = "body")
    @PostMapping("saveHospitalSet")
    public Result<HospitalSet> saveHospitalSet(@RequestBody(required = true) HospitalSet hospitalSet) {
        if (StringUtils.isEmpty(hospitalSet.getHosname())||StringUtils.isEmpty(hospitalSet.getHoscode())
                ||StringUtils.isEmpty(hospitalSet.getApiUrl())||StringUtils.isEmpty(hospitalSet.getContactsName())
                ||StringUtils.isEmpty(hospitalSet.getContactsPhone())) {
            return Result.fail();
        }
        //设置状态  1 使用  0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    //5 根据id获取医院设置
    @ApiOperation(value = "根据id获取医院设置")
    @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", paramType = "path")
    @GetMapping("/getHospSet/{id}")
    public Result<HospitalSet> getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //6 修改医院设置
    @ApiOperation(value = "修改医院设置")
    @ApiImplicitParam(name = "hospitalSet", value = "修改的条件", dataType = "JSON", paramType = "body")
    @PutMapping("/updateHospitalSet")
    public Result<?> updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @ApiImplicitParam(name = "idList", value = "删除的id集合", dataType = "list", paramType = "body")
    @DeleteMapping("/·")
    public Result<?> batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        boolean flag = hospitalSetService.removeByIds(idList);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //8 解锁 锁定医院设置
    @ApiOperation("解锁 锁定医院设置")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", dataType = "Long", paramType = "path"), @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "path")})
    @GetMapping("/lockHospitalSet/{id}/{status}")
    public Result<?> lockHospitalSet(@PathVariable Long id,
                                     @PathVariable Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);

        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //发送签名key
    @ApiOperation("发送签名key")
    @GetMapping("/sendKey/{id}")
    public Result<?> sendSignKey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        //hoscode
        String hoscode = hospitalSet.getHoscode();
        //signKey
        String signKey = hospitalSet.getSignKey();
        // TODO发送
        //....
        return Result.ok();
    }


}